

package multiNyID.Interaksi;

import javax.vecmath.Point2d;
import javax.vecmath.Point2i;
import javax.vecmath.Point3d;
import javax.vecmath.Point4d;
import multiNyID.MarkerModel.MarkerModelNyID;

/**
 * class OcclusionDetection
 *
 * khusus untuk presentasi TA
 *
 * @author whe_green@yahoo.co.id
 * Analisis Metode Occlusion Based Pada Augmented Reality
 * Studi Kasus Interaksi dengan Objek Virtual Secara Realtime
 * Menggunakan Gerakan Tangan
 *
 * Institut Teknologi Telkom
 * Fakultas Informatika 2007
 * Bandung Indonesia
 *
 * nyargreen.wordpress.com
 */

public class IntroOcclusion implements InteraksiInterface{
        private Point4d batas;
        // maxX = batas.x
        // minX = batas.y
        // maxY = batas.Z
        // minY = batas.w

        //scale = 0.275
        //skala papancatur = scale/2 = 0,138
        //panjang sisi papancatur = 8 * 0,138
        //radius = panjang sisi / (2*ukuran marker + jarak marker)
        
        private Point2d ptr1,ptr2,pusatGrid;

        private Point2i ordinat = new Point2i();

//        private final double radius = 0.18/1.415;  //  1 cm --> 0,01 jadi 32 cm = 0.32
        private final double radius = 0.26/1.415;  //  1 cm --> 0,01 jadi 32 cm = 0.32
//        private final double radius = 0.36/1.415;  //  1 cm --> 0,01 jadi 32 cm = 0.32
        private final double radiusPtr = 0.1/1.415;

        private boolean onAmbil;
        
    public IntroOcclusion() {
    }

    public void indikatorPointer(MarkerModelNyID grid,MarkerModelNyID ptr1,
            MarkerModelNyID ptr2,StringBuffer stat) {

        if((ptr1==null)||(grid==null)){
            System.out.println("null");
            return;
        }
        pusatGrid = konvPtr(grid.getPos());
        this.ptr1 = konvPtr(ptr1.getPos());

        geserPointer(grid, stat);
    }
//hanya cek occlusion atau ga? cuma satu pointer aja!!
    public void geserPointer(MarkerModelNyID grid, StringBuffer stat){

        batas = hitBatas(pusatGrid, radius); //bikin kotak seluas papan catur

        if (isOcclusion(ptr1, batas)){
            System.out.println("----------> OCCLUSION");
            if(!grid.lock[0]){ //
                
                //perintah
//                grid.getSlide().nextModel();
                if(!grid.lock[1]){ //normal
                    grid.getModel().getModelPropMan().scale(2);
                    grid.lock[1] = true;
                }
                else{ //udah ke *oom
                    grid.getModel().getModelPropMan().scale(0.5);
                    grid.lock[1] = false;
                }
                
                grid.lock[0] = true;
                
            }
        }

        else {
            System.out.println("----------> GA OCCLUSION");

                grid.lock[0] = false;
                
            }
    }

    public boolean lebihDari(double a,double b)
    {
        return (a>b) ? true : false;
    }  


    public Point2d konvPtr(Point3d t){
        Point2d p = new Point2d();
        p.setX(t.getX());
        p.setY(t.getY());
        return p;
    }

    public Point4d hitBatas(Point2d pusatGrid, double jarak){
       Point4d p = new Point4d(pusatGrid.getX()+(jarak*100), pusatGrid.getY()+(jarak*100),
                pusatGrid.getX()-(jarak*100), pusatGrid.getY()-(jarak*100));
       return p;
    }
    public boolean isOcclusion(Point2d ptr, Point4d batas){
        double a = ptr.getX();
        double b = ptr.getY();
        double maxA = batas.getX();
        double maxB = batas.getY();
        double minA = batas.getZ();
        double minB = batas.getW();
        return((lebihDari(a, minA)&&lebihDari(maxA, a)&&lebihDari(b, minB)&&lebihDari(maxB, b))==true) ? true : false;
    }

}
